/**
 * Created by sunyxq on 16/3/29.
 */
angular.module('hyt.findXianLuService',[])

    .factory('XlListFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
        var api =  Storage.get('fwqdz'),
            pagesize = 10,
            xlData = {};
        console.log(api);

        var resource = $resource(api);

        return {
            getXlList : function(sid,user_id,yh_id,xlmc){

                var hasNextPage = true;

                return resource.save({
                    'dataType':'yw',
                    'ACTION_NAME':'sqlKeyBiz.page#wlxianlu_query',
                    'SID':sid,
                    'USER_ID':user_id,
                    'ACTION_INFO': {
                        'pagesize': pagesize,
                        'current_page':'1',
                        'yh_id':yh_id,
                        'xlmc':xlmc
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == '000000'){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }

                        xlData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : '2',
                            'data' : r.ACTION_INFO.RESULT_LIST
                        };
                        console.log( xlData[pagesize] );
                        $rootScope.$broadcast('XlList.listUpdate');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },

            getXl : function(){
                if( xlData[pagesize] == undefined ){
                    return false;
                }

                return xlData[pagesize];
            },



            getMoreXl:function(sid,user_id,yh_id,xlmc){

                //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
                if(xlData[pagesize] == undefined){
                    return false;
                }

                //获取以前的数据
                var hasNextPage = xlData[pagesize].hasNextPage;
                var current_page = xlData[pagesize].current_page;
                var moreXlData = xlData[pagesize].data;

                //console.log(hasNextPage);

                resource.save({
                    'dataType':'yw',
                    'SID':sid,
                    'USER_ID':user_id,
                    'ACTION_NAME':'sqlKeyBiz.page#wlxianlu_query',
                    'ACTION_INFO': {
                        'pagesize': pagesize,
                        'current_page':current_page,
                        'yh_id':yh_id,
                        'xlmc':xlmc
                    }
                }, function (r) {

                    console.log(r);
                    current_page++;

                    if(r.ACTION_RETURN_CODE == "000000"){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }

                        moreXlData = moreXlData.concat(r.ACTION_INFO.RESULT_LIST);

                        xlData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : current_page,
                            'data' : moreXlData
                        };

                        $rootScope.$broadcast('XlList.listUpdate');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }


                })
            },

            hasNextPage: function() {
                if (xlData[pagesize] === undefined) {
                    return false;
                }
                return xlData[pagesize].hasNextPage;
            }
        }
    })

    .factory('XlFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
        var api =  Storage.get('fwqdz'),
            xlChangeData = {};

        var resource = $resource(api);

        return {
            getChange : function(id){
                return resource.save({
                    'dataType':'yw',
                    'ACTION_NAME':'beanBiz.fetch#Wlyxlgl',
                    'ACTION_INFO': {
                        'id': id
                    }
                },function(r){
                    //console.log(r);
                    if(r.ACTION_RETURN_CODE == '000000'){
                        xlChangeData = r.ACTION_INFO;
                        //console.log(xlChangeData);
                        $rootScope.$broadcast('XlChange.changeUpdate');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },

            getChangeContent : function(){
                if( xlChangeData == undefined){
                    return false;
                }
                return xlChangeData
            },

            saveChange : function(id,xlmc,gsmc,sjhm,lxdh,wlyid){
                console.log(id,xlmc,gsmc,sjhm,lxdh);
                return resource.save({
                    'dataType' : 'yw',
                    'ACTION_NAME' : 'beanBiz.merge#Wlyxlgl',
                    'ACTION_INFO': {
                        '_options' :{
                            'mergeField' : 'xl_id',
                        },
                        'xl_id': id,
                        'xlmc' : xlmc,
                        'wlgsmc' : gsmc,
                        'sjhm' : sjhm,
                        'plane' : lxdh
                        ,
                        'wlyid' : wlyid
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == '000000'){
                        xlChangeData = r.ACTION_INFO;
                        $rootScope.$broadcast('XlChanged.changedUpdate');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            }
        }
    });